/**
 *	VisualGroup JavaScript prototype Classes, version draf
 *  (c) 2008 Bill Vison <vison_ruan@126.com>
 *
 *
/*--------------------------------------------------------------------------*/
using("Vsg.DataSourceEntity");
using("Vsg.EntityRecord");

/**
 * @constructor
 */
Vsg.DataSourceEntity.DataSourceEntityBuilder = function() {
	
	/** DataSourceEntityBuilder constructor init attribute */
	var $_attrs = {};
	
	Vsg.DataSourceEntity.DataSourceEntityBuilder.superclass.constructor.call(this,$_attrs);
	
	/** implement interface method "newLoadedDataSource" */
	this.newLoadedDataSource = function($config) {
		var $_limit = 20;
		var $_params = this.$_params;
		try {
			// --- check mpt ---
			if (this.getUrl() == null) {
				throw "没有定义数据源 'URL' 连接!";
			}
			
			if (this.getEntityRecord() == null) {
				throw "没有定义数据映射对像!";
			}
			
			var $readerSet = {
				root:"rows",
				totalProperty:"results",
				id:"id"
			}

			var $dataStoreSet = {};

			var $ds = null;

			// ---- construct method ----
			var $reader = new Ext.data.JsonReader( $readerSet , this.getEntityRecord().createEntity());
			$dataStoreSet.reader = $reader;
			$dataStoreSet.baseParams = $_params;
			$dataStoreSet.proxy = new Ext.data.ScriptTagProxy({url:this.getUrl()});
			$dataStoreSet.remoteSort = true;

			var $ds = new Ext.data.Store($dataStoreSet);
			
			// --- append config defined ---
			if ($config != null ) {
				if ($config.autoload) {
					$ds.load();
				}
			}
			
			return $ds;

		} catch (e) {
		
			// -- alert frame test
			throw e;

		}
	}
	
	/** */
	this.newJsonReader = function() {
		var $readerConf = {
			root:"data",
			successProperty:"success",
			id:"id"
		};
	
		var $reader = new Ext.data.JsonReader( $readerConf , this.getEntityRecord().createEntity());	
		
		return $reader;
	}


	/** implement interface method "newGroupDataSource" */
	this.newGroupDataSource = function($args) {
		var $_limit = 20;
		var $_params = this.$_params;
		try {
			// --- check mpt ---
			if (this.getUrl() == null) {
				throw "没有定义数据源连接!";
			}
			
			if (this.getEntityRecord() == null) {
				throw "没有定义数据映射对像!";
			}
			
			
			var $readerSet = {
				root:"rows",
				totalProperty:"results",
				id:"id"
			}

			var $dataStoreSet = {};

			var $ds = null;

			// ---- construct method ----
			var $reader = new Ext.data.JsonReader( $readerSet , this.getEntityRecord().createEntity());
			$dataStoreSet.reader = $reader;
			$dataStoreSet.baseParams = $_params;
			$dataStoreSet.proxy = new Ext.data.ScriptTagProxy({url:this.getUrl()});
			$dataStoreSet.remoteSort = true;
			$dataStoreSet.groupField = $args.groupField;
			$dataStoreSet.sortInfo = $args.sortInfo;
		
			
			var $ds = new Ext.data.GroupingStore($dataStoreSet);
			
			return $ds;

		} catch (e) {
		
			// -- alert frame test
			throw e;

		}

	}
	
	this.newAjaxDataSource = function(args) {
		var $_limit = 20;
		var $_params = this.$_params;
		try {
			// --- check mpt ---
			if (this.getUrl() == null) {
				throw "没有定义数据源 'URL' 连接!";
			}
			
			if (this.getEntityRecord() == null) {
				throw "没有定义数据映射对像!";
			}
		
			var Entity = this.getEntityRecord().createEntity();

			var tmpReader = new Ext.data.JsonReader({
					root:"rows",
					totalProperty:"results",
					id:"id"
				},Entity);
				
			var $ds = new Ext.data.Store({
    			// explicitly create reader
    			reader: new Ext.data.ArrayReader(
        		{
            		idIndex: 0  // id for each record will be the first element
        		},
        		Entity // recordType
    			)
			});

			Ext.Ajax.request({
   				url: this.getUrl(),
   				success: function(response, opts) {
      				//var evl = eval("("+response.responseText+");");
					var evl = Ext.decode(response.responseText);
					var recs = tmpReader.readRecords(evl);
					var recArray = recs['records'];
					$ds.add(recArray);
				},
   				failure: function(response, opts) {
					alert('server-side failure with status code ' + response.status);
				},
   				params: $_params
			});
						
			return $ds;

		} catch (e) {
		
			// -- alert frame test
			throw e;

		}		
	}


}



// --- set the override
var $overrideMethods = new Object();

// extend panel
Ext.extend(Vsg.DataSourceEntity.DataSourceEntityBuilder, Ext.util.Observable, $overrideMethods);

/** set the property */
Vsg.DataSourceEntity.DataSourceEntityBuilder.prototype = {
	/** defined variable */
	$_url:null,
	$_params:{},
	$_entityRecord:null,
	
	/** getter method */
	getUrl:function() {
		return this.$_url;
	},
	
	/** implement interface method "setUrl" */
	setUrl:function($url , $params) {
		this.$_url = $url;
		if ($params != null) {
			for (var $i in $params) {
				this.$_params[$i] = $params[$i];
			}
		}
	},
	
	/** implement interface method "setEntityRecord" */
	setEntityRecord:function($entityRecord) {
		// --- check the interface
		try {
			Interface.ensureImplements($entityRecord  , Vsg.EntityRecord );
			this.$_entityRecord = $entityRecord;
		} catch (e) {
			alert(e);
		}
	},
	
	/** getter method */
	getEntityRecord:function() {
		return this.$_entityRecord;
	}
}

/** check interface implement */
Ext.onReady(function() {
	try {
		Interface.ensureImplements(new Vsg.DataSourceEntity.DataSourceEntityBuilder(), Vsg.DataSourceEntity);	
	} catch (e) {
		Ext.log("Class Implement Error :"+"[Vsg.DataSourceEntity.DataSourceEntityBuilder] , " + e);
	}
});